package com.peng.androidlib.test;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.ToastUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.peng.androidlib.R;
import com.peng.androidlib.appbase.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;

public class MainActivity extends BaseActivity {

    @BindView(R.id.showToast)
    Button showToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void initViews() {
        RxView.clicks(showToast).throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(o -> {
                    ToastUtils.showShort("123");
                });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void handleBundleExtras(@NonNull Bundle extras) {

    }

    @Override
    protected void handleSavedInstanceState(@NonNull Bundle savedInstanceState) {

    }

    private Dog dog;
    private Realm mRealm;

    public void add(View view) {
        mRealm = Realm.getDefaultInstance();

        for (int i = 0; i < 10; i++) {
            dog = new Dog();
            dog.setName("狗一");
            dog.setId(String.valueOf(i));
            dog.setAge(i);
        }

        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.copyToRealmOrUpdate(dog);
            }
        });
    }

    public void delete(View view) {

        mRealm = Realm.getDefaultInstance();
        final RealmResults<Dog> dogs = mRealm.where(Dog.class).findAll();
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {

                Dog dog = dogs.get(5);
                dog.deleteFromRealm();
                //删除第一个数据
                dogs.deleteFirstFromRealm();
                //删除最后一个数据
                dogs.deleteLastFromRealm();
                //删除位置为1的数据
                dogs.deleteFromRealm(1);
                //删除所有数据
                dogs.deleteAllFromRealm();
            }
        });
    }

    public void update(View view) {
        mRealm = Realm.getDefaultInstance();
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到User对象
                Dog dog = mRealm.where(Dog.class).findFirst();
                dog.setAge(26);
            }
        });

    }

    public void query(View view) {
        mRealm = Realm.getDefaultInstance();
        RealmResults<Dog> digList = mRealm.where(Dog.class).equalTo("name", "狗一").findAllAsync();
        Log.d("dog", digList.get(0).toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRealm.close();
    }
}
